# This script performs initial analysis of the PE network
# Last edited: 9/24/2013
# Edited by: Bruce Desmarais

vertDat <- read.csv("~/Dropbox/professional/Research/Active/SenPE/Data/cleanData/DSMKdata.csv")
setwd("/Users/brucedesmarais/Dropbox/professional/Research/Active/SenPE/Data/cleanData/Netobjects/")
networks <- list.files()
for(i in 1:length(networks)){
	load(networks[i])
}

# Creating a list of networks
nets <- list(net7980,net8182,net8384,net8586,net8788,net8990,net9192,net9394,net9596,net9798,net0304,net0506,net0708)
names(nets) <- c(96:105,108:110)
library(network)

## Vertex checking whether the vertex sets in the networks match the node-level dataset
misDat <- NULL
for(i in 1:length(nets)){
	neti <- nets[[i]]
	congi <- names(nets)[i]
	namesi <- colnames(neti[,])
	sens <- namesi[which(nchar(namesi)>4)]
	senv <- as.character(vertDat$ICPSR[which(vertDat$Congress==as.numeric(names(nets)[i]))])
	sens <- substr(sens,1,nchar(sens)-2)
	inVnotN <- senv[which(!is.element(senv,sens))]
	inNnotV <- sens[which(!is.element(sens,senv))]
	missi <- c(congi,paste(inVnotN,collapse=":"),paste(inNnotV,collapse=":"))
	misDat <- rbind(misDat,missi)
}
colnames(misDat) <- c("cong","inVnotN","inNnotV")

## misDat should be blank


### collapse networks onto senators via the number of events attended together
netList <- list()
for(i in 1:length(nets)){
	neti <- nets[[i]]
	congi <- names(nets)[i]
	namesi <- colnames(neti[,])
	vdati <- vertDat[which(vertDat$Congress==as.numeric(names(nets)[i])),]
	senID <- vdati$ICPSR
	amati <- matrix(0,length(senID),length(senID))
		for(j in 2:length(senID)){
			senj <- senID[j]
			indj <- which(as.numeric(substr(namesi,1,nchar(namesi)-2))==senj)
			for(k in 1:(j-1)){
				senk <- senID[k]
				indk <- which(as.numeric(substr(namesi,1,nchar(namesi)-2))==senk)
				amati[j,k] <- amati[k,j] <- sum(neti[indj,]*neti[indk,])
			}
		}
	netLIst[[i]] <- list(amat=amati,vdat=vdati)
	print(i)
}
